package cn.lgs.com.mobileplayer.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import butterknife.ButterKnife;

/**
 * Created by lgs on 2017/8/24.
 * Fragment基类
 */

public abstract class BaseFragment extends Fragment {

    private View mRootView;

    protected Fragment mCurrentFragment;  //当前显示的Fragment

    /**
     * 这个方法提供初始化的布局id
     *
     * @return 布局id
     */
    protected abstract int getLayoutId();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == mRootView) {
            mRootView = inflater.inflate(getLayoutId(), container, false);
        } else {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (null != parent) {
                parent.removeView(mRootView);
            }
        }
        ButterKnife.bind(this, mRootView);
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    /**
     * 初始化成员属性，不需要手动调用，只需要重写内容即可
     */
    protected abstract void init();

    /**
     * 吐司提示
     *
     * @param msg 信息字段
     */
    protected void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 吐司提示
     *
     * @param msg      信息字段
     * @param fragment 需要吐司的当前Fragment
     */
    protected void showMsg(String msg, BaseFragment fragment) {
        if (fragment != null && fragment.isAdded()) {
            Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 返回到上一个Fragment
     */
    protected void back() {
        getFragmentManager().popBackStack();
    }

    /**
     * 统一构造子类
     *
     * @param className 类名
     * @param bundle    数据
     * @param <T>       泛型
     * @return 返回值
     */
    public static <T extends BaseFragment> T newInstance(Class<T> className, Bundle bundle) {
        T instance = null;
        try {
            instance = className.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != bundle && null != instance) {
            instance.setArguments(bundle);
        }
        return instance;
    }

    /**
     * Fragment跳转，将一个layout替换为新的fragment
     *
     * @param fm
     * @param fragmentClass
     * @param replaceLayoutId
     * @param args
     */
    protected static void replace(FragmentManager fm, Class<? extends Fragment> fragmentClass,
                                  int replaceLayoutId, Bundle args) {

        replace(fm, fragmentClass, replaceLayoutId, fragmentClass.getSimpleName(), args);
    }

    /**
     * Fragment跳转，将一个layout替换为新的fragment
     *
     * @param fm
     * @param replaceLayoutId
     * @param fragmentClass
     */
    protected static Fragment replace(FragmentManager fm, int replaceLayoutId,
                                      Class<? extends Fragment> fragmentClass) {

        return replace(fm, fragmentClass, replaceLayoutId, fragmentClass.getSimpleName(), null);
    }

    /**
     * Fragment跳转， 将一个layout替换为新的fragment
     *
     * @param fm
     * @param fragmentClass
     * @param replaceLayoutId
     * @param tag
     * @param args
     * @return
     */
    protected static Fragment replace(FragmentManager fm, Class<? extends Fragment> fragmentClass,
                                      int replaceLayoutId, String tag, Bundle args) {

        //如果这个Fragment没有通过tag找到，说明此Fragment不存在，所以这里重新添加一个，在这时就指定了它的tag了
        Fragment fragment = fm.findFragmentByTag(tag);
        boolean isFragmentExist = true;
        //Fragment为空时
        if (null == fragment) {
            isFragmentExist = false;
            try {
                fragment = fragmentClass.newInstance();
                if (null != args) {
                    fragment.setArguments(args);
                } else {
                    fragment.setArguments(new Bundle());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            //Fragment不为空时
        } else {
            if (null != args) {
                if (null != fragment.getActivity()) {
                    fragment.getArguments().putAll(args);
                } else {
                    fragment.setArguments(args);
                }
            }
        }
        if (null == fragment) {
            return null;
        }
        if (fragment.isAdded()) {
            return fragment;
        }
        FragmentTransaction ft = fm.beginTransaction();
        if (isFragmentExist) {
            ft.replace(replaceLayoutId, fragment);
        } else {
            ft.replace(replaceLayoutId, fragment, tag);
        }

        ft.addToBackStack(tag);
        ft.commitAllowingStateLoss();
        return fragment;
    }


    /**
     * @param from 当前显示的Fragment, 即将被隐藏
     * @param to   将要切换到的Fragment，即将要显示
     */
    protected void switchFragment(Fragment from, Fragment to, int containerViewId) {

        FragmentTransaction ft = getChildFragmentManager().beginTransaction();

        if (from != to) {
            mCurrentFragment = to;

            //to没有被添加的情况
            if (!to.isAdded()) {

                if (null != from) {
                    //隐藏from
                    ft.hide(from);
                }
                if (null != to) {
                    //添加to
                    ft.add(containerViewId, to).commit();
                }
                //to已被添加的情况
            } else {
                if (null != from) {
                    ft.hide(from);
                }
                if (null != to) {
                    //显示to
                    ft.show(to).commit();
                }
            }
        }
    }

}
